package XML;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;


public class XMLDataDemo {









//public static void main(String arge[]){

        public static void mod(String h,String i) {

            File file = new File("D:\\GitHOME\\information\\text.xml");
            DocumentBuilder newDocumentBuilder = null;
            try {
                newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            }
            Document doc = null;
            try {
                doc = newDocumentBuilder.parse(file);
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Element root = doc.getDocumentElement();
            //查找指定节点
            //通过根节点获得子节点
            NodeList personList = root.getElementsByTagName("spot");
        /*    System.out.println("请输入一个整数：");
            Scanner scan = new Scanner(System.in);
            int i=scan.nextInt();*/
            Node item = personList.item(2);
            Element personElement = (Element) item;
            //personElement.
            NodeList idList = personElement.getElementsByTagName("id");
            //修改
           /* System.out.println("请输入编号：");
            Scanner sc = new Scanner(System.in);
            String h=sc.next();*/
            idList.item(0).setTextContent(h);
            NodeList introList = personElement.getElementsByTagName("intro");
         /*   System.out.println("请输入简介：");
            Scanner si = new Scanner(System.in);
            String i=si.next();*/
            introList.item(0).setTextContent(i);
            Transformer transformer = null;
            try {
                transformer = TransformerFactory.newInstance().newTransformer();
            } catch (TransformerConfigurationException e) {
                e.printStackTrace();
            }
            Source source = new DOMSource(doc);
            Result result = new StreamResult(file);
            try {
                transformer.transform(source, result);
            } catch (TransformerException e) {
                e.printStackTrace();
            }
        }



    }


